import { request } from '../request'
import type {
    User,
    LoginRequest,
    LoginResponse,
    RegisterRequest,
    UpdateUserRequest,
    ChangePasswordRequest,
    PageRequest,
    PageResponse
} from '@/types'

// 用户登录
export const login = (data: LoginRequest): Promise<LoginResponse> => {
    return request.post('/api/user-service/api/user/login', data)
}

// 用户注册
export const register = (data: RegisterRequest): Promise<void> => {
    return request.post('/api/user-service/api/user/register', data)
}

// 获取当前用户信息
export const getCurrentUser = (): Promise<User> => {
    return request.get('/api/user-service/api/user/me')
}

// 更新用户信息
export const updateUserProfile = (data: UpdateUserRequest): Promise<void> => {
    return request.put('/api/user-service/api/user/update', data)
}

// 修改密码 (注意：这个接口在接口文档中没有找到，可能需要后端补充)
export const changePassword = (data: ChangePasswordRequest): Promise<void> => {
    return request.put('/api/user-service/api/user/password', data)
}

// 获取用户积分
export const getUserPoints = (): Promise<number> => {
    return request.get('/api/user-service/api/user/points')
}

// 刷新Token
export const refreshToken = (refreshToken: string): Promise<LoginResponse> => {
    return request.post('/api/user-service/api/token/refresh', null, {
        params: { refreshToken }
    })
}

// 用户登出
export const logout = (refreshToken: string): Promise<void> => {
    return request.post('/api/user-service/api/token/logout', null, {
        params: { refreshToken }
    })
}

// ================= 后台 API =================

// 分页查询后台用户列表
export const getAdminUserList = (params: PageRequest & { keyword?: string }): Promise<PageResponse<User>> => {
    return request.get('/api/user-service/admin/user/list', { params })
}

// 更新用户状态
export const updateUserStatus = (id: number, status: number): Promise<void> => {
    return request.put(`/api/user-service/admin/user/${id}/status`, null, { params: { status } })
}
